﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.ServiceModel.DomainServices.Server;
using NetBay.Core.BusinessEntities;
using NetBay.Core.Log;
using Rio.Applications.Web.Services.BusinessEntities;
using System.Web;
using Rio.Framework.RioManager;
using Rio.Doc.Business.Repository;
#endregion


namespace Rio.Applications.Web.Services
{
    /// <summary>
    /// Class to handle Business entities in a RIA services
    /// </summary>
    public partial class BusinessEntitiesDomainService : DomainService
    {

        /// <summary>
        /// Update a status of an application on a computer
        /// </summary>
        /// <param name="p_sApplicationName">The application Name</param>
        /// <param name="p_bApplicationInstalled">Does the application is installed</param>
        [Invoke]
        public void UpdatePcApplicationStatus(string p_sApplicationName, Boolean p_bApplicationInstalled)
        {
            try
            {
                var l_sComputerName = string.Empty;
                var l_sComputerIPAddress = string.Empty;

                if (SettingsManager.cStoreSettings.ScanIdentifier.Equals("0"))
                {
                    l_sComputerName = HttpContext.Current.Request.UserHostName;
                    l_sComputerIPAddress = HttpContext.Current.Request.UserHostAddress;
                }
                else
                {
                    l_sComputerName = GetUser().Login;
                    l_sComputerIPAddress = GetUser().Login;
                }
                LogManager.Instance().LogDebug("UpdatePcApplicationManagement (<p_sApplicationName<" + p_sApplicationName + ">p_bApplicationInstalled<" + p_bApplicationInstalled + ">)...");
                AdministrationRepository.UpdatePcApplicationStatus(l_sComputerName, p_sApplicationName, l_sComputerIPAddress, p_bApplicationInstalled);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("Exception in UpdatePcApplicationStatus : " + ex.Message);
            }
        }


        /// <summary>
        /// Update a status of an application on a computer
        /// </summary>
        /// <param name="p_sApplicationName">The application Name</param>
        /// <returns> A ReturnBoolean object corresponding to the status</returns>
        [Invoke]
        public ReturnBoolean GetPcApplicationStatus(string p_sApplicationName)
        {
            return GetPcApplicationStatusInternal(p_sApplicationName, string.Empty);
        }

        /// <summary>
        /// Gets the pc application status.
        /// </summary>
        /// <param name="p_sApplicationName">Name of the P_S application.</param>
        /// <param name="p_sLoginName">Name of the P_S login.</param>
        /// <returns></returns>
        public ReturnBoolean GetPcApplicationStatusInternal(string p_sApplicationName, string p_sLoginName)
        {
            ReturnBoolean l_oReturnObject;
            var l_sComputerName = string.Empty;
            if (SettingsManager.cStoreSettings.ScanIdentifier.Equals("0"))
            {
                l_sComputerName = HttpContext.Current.Request.UserHostName;
            }
            else
            {
                try
                {
                    
                    l_sComputerName = !"".Equals(p_sLoginName) ? p_sLoginName : GetUser().Login;
                }
                catch { }
            }

            try
            {
                LogManager.Instance().LogDebug("(p_sComputerName<" + l_sComputerName + "> p_sApplicationName<" + p_sApplicationName + ">)...");
                l_oReturnObject = AdministrationRepository.GetPcApplicationStatus(l_sComputerName, p_sApplicationName);
            }
            catch (Exception ex)
            {
                l_oReturnObject = new ReturnBoolean("Exception in UpdatePcApplicationStatus : " + ex.Message);
                LogManager.Instance().LogError(l_oReturnObject.FullErrorMessage);
            }

            return l_oReturnObject;
        }

    }
}